package nmp.remote;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;

import nmp.gui.CWifiConfigDlg;
import nmp.lib.CNmpCmd;
import nmp.lib.CNmpCmd.*;
import nmp.lib.CNmpDevice;
import nmp.lib.CNmpDongle;
import nmp.lib.CNmpJsonCmd;
import nmp.lib.CNmpTcp;
import nmp.lib.CNmpUdp;
import nmp.lib.CNmpWifiManage;
import android.animation.AnimatorSet.Builder;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class NmpTest extends Activity 
{    
	private static final String NMP_TEST_CONNECT = "Connect";
	private static final String NMP_TEST_DISCONNECT = "Disconnect";
	private static final String NMP_TEST_IDENTIFY = "Identify";
	private static final String NMP_TEST_WIFILIST = "Get Wifi List";
	private static final String NMP_TEST_WIFICONNECT = "Wifi Connect";
	private static final String NMP_TEST_WIFIDISCONNECT = "Wifi Disconnect";
	private static final String NMP_TEST_DEVICEINFO = "Wifi DeviceInfo";
	private static final String NMP_TEST_WIFICONFIG = "Wifi Config";
	private static final String NMP_TEST_WIFIISCONNECTED = "Wifi IsConnected";
	private static final String NMP_TEST_GET_SSIDPASSWORD_LIST = "Get Pref. Password";
	private static final String NMP_TEST_RECONNECT_SSID = "Reconnect to SSID";
	private static final String NMP_TEST_ALARMDIALOG = "Alarm Dialog";
	private static final String NMP_TEST_UDP = "UDP Test";
	private static final String NMP_TEST_TCP = "TCP Test";
		
	private enum CONTROL_STATE {
		NONE,
		READY
	};
	
	private ProgressDialog m_busy_dialog = null;
	private Context m_context;
	private Button mButtonStart;
	private Button mButtonSend;
	private Button mButtonStop;
	private TextView mTextView;
	private Spinner mSpinner;
	private String textViewString;
	private String mSelectString;
	//protected static final int NMP_PORT = 3901;
	private CNmpUdp mUdpClient;
	private CNmpUdp mUdpServer;
	private CNmpTcp mTcpServer;
	private CNmpTcp mTcpClient;
	private CWifiConfigDlg mConfigDlg;
	private CNmpDongle mNmpDevice = null;
	private CNmpWifiManage mWifiManage;
	
	final NmpWifiInfo m_connect_wifiinfo = new NmpWifiInfo();
	final NmpWifiInfo m_configure_wifiinfo = new NmpWifiInfo();
	CNmpCmd    mCmd;
	private int m_f_status = -1;
	private CONTROL_STATE mState;
	
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	Log.d(this.toString(),"NmpTest onCreate start");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.unit_test);
        
        m_context = this;
        mSelectString = null;
        mButtonStart = (Button) findViewById(R.id.btnTestStart); 
        mButtonSend = (Button) findViewById(R.id.btnTestSend);
        mButtonStop = (Button) findViewById(R.id.btnTestStop); 
        mTextView = (TextView) findViewById(R.id.textViewTest);
        mSpinner = (Spinner) findViewById(R.id.spinnerTestList);
        mConfigDlg = new CWifiConfigDlg(this);
        mWifiManage = new CNmpWifiManage(this);
        
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,
        		new String[]{NMP_TEST_CONNECT,
        		             NMP_TEST_DISCONNECT,
        		             NMP_TEST_IDENTIFY,
        		             NMP_TEST_WIFILIST,
        		             NMP_TEST_WIFICONNECT,
        		             NMP_TEST_WIFIDISCONNECT,
        		             NMP_TEST_DEVICEINFO,
        		             NMP_TEST_WIFICONFIG,
        		             NMP_TEST_WIFIISCONNECTED,
        		             NMP_TEST_GET_SSIDPASSWORD_LIST,
        		             NMP_TEST_RECONNECT_SSID,
        		             NMP_TEST_ALARMDIALOG,
        		             NMP_TEST_UDP,
        		             NMP_TEST_TCP});

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);
        
        mSpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener() {

			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				mSelectString = arg0.getSelectedItem().toString();				
				if(mSelectString.equals(NMP_TEST_CONNECT)) {					
			    } else if (mSelectString.equals(NMP_TEST_DISCONNECT)) {	
			    } else if (mSelectString.equals(NMP_TEST_IDENTIFY)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFILIST)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFICONNECT)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFIDISCONNECT)) {
			    } else if (mSelectString.equals(NMP_TEST_DEVICEINFO)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFICONFIG)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFIISCONNECTED)) {
			    } else if (mSelectString.equals(NMP_TEST_GET_SSIDPASSWORD_LIST)) {
			    } else if (mSelectString.equals(NMP_TEST_RECONNECT_SSID)) {
			    } else if (mSelectString.equals(NMP_TEST_ALARMDIALOG)) {
			    } else if (mSelectString.equals(NMP_TEST_UDP)) {			    	
			    } else if (mSelectString.equals(NMP_TEST_TCP)) {			    	
			    }		    
				textViewString = "Select test command: " +mSelectString;
				mTextView.setText(textViewString);
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
			}
        });
        
        mButtonStart.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				
				    textViewString = " start the command:" + mSelectString ;
					if(mSelectString.equals(NMP_TEST_CONNECT)) {
						NmpDeviceOpen();
				    } else if (mSelectString.equals(NMP_TEST_DISCONNECT)) {
				    	NmpDeviceClose();
				    } else if (mSelectString.equals(NMP_TEST_IDENTIFY)) {
				    	if(!CheckConnected()) return;
				    	mNmpDevice.SendIdentifyCmd(android.os.Build.MODEL, 1000);
				    } else if (mSelectString.equals(NMP_TEST_WIFILIST)) {
				    	if(!CheckConnected()) return;
					    mNmpDevice.SendGetWifiListCmd();
					    m_busy_dialog.show();
				    } else if (mSelectString.equals(NMP_TEST_WIFICONNECT)) {
				    	if(!CheckConnected()) return;
				    	CWifiConfigDlg dlg = new CWifiConfigDlg(m_context,m_connect_wifiinfo){
				    		public void Result(NmpWifiInfo wifiinfo) {
				    			
				    			if(wifiinfo == null) {
				    				textViewString="Cancel ";
				    				mTextView.setText(textViewString);	
				    			} else {
									textViewString="";
									textViewString += "[ssid]:"+ wifiinfo.ssid +"\n";
									textViewString += "[protect]:"+ wifiinfo.protect +"\n";
									textViewString += "[password]:"+ wifiinfo.password +"\n";
									mTextView.setText(textViewString);	
									mNmpDevice.SendWifiConnectCmd(wifiinfo.ssid, wifiinfo.protect, wifiinfo.password);
				    			}
				    		}				    		
				    	};
				    	dlg.SetPassword("1234567890");
				    	dlg.SetProtect("WPA");
				    	dlg.SetSSID("miiCasa_router");
				    	dlg.show();			
				    } else if (mSelectString.equals(NMP_TEST_WIFIDISCONNECT)) {
				    	if(!CheckConnected()) return;
				    	mNmpDevice.SendWifiDisconnectCmd();
				    } else if (mSelectString.equals(NMP_TEST_DEVICEINFO)) {
				    	if(!CheckConnected()) return;
				    	mNmpDevice.SendDeviceInfoCmd();
				    } else if (mSelectString.equals(NMP_TEST_WIFICONFIG)) {		
				    	if(!CheckConnected()) return;
				    	CWifiConfigDlg dlg = new CWifiConfigDlg(m_context,m_configure_wifiinfo){
				    		public void Result(NmpWifiInfo wifiinfo) {
				    			if(wifiinfo == null) {
				    				textViewString="Cancel";
				    				mTextView.setText(textViewString);	
				    			} else {
									textViewString="";
									textViewString += "[ssid]:"+ wifiinfo.ssid +"\n";
									textViewString += "[protect]:"+ wifiinfo.protect +"\n";
									textViewString += "[password]:"+ wifiinfo.password +"\n";
									mTextView.setText(textViewString);	
									mNmpDevice.SendWifiConfigCmd(wifiinfo.ssid, wifiinfo.protect, wifiinfo.password);									
				    			}
				    		}				    		
				    	};		
				    	dlg.show();	
				    } else if (mSelectString.equals(NMP_TEST_WIFIISCONNECTED)) {
				    	if(!CheckConnected()) return;
				    	mNmpDevice.SendWifiIsConnectedCmd();
				    } else if (mSelectString.equals(NMP_TEST_GET_SSIDPASSWORD_LIST)) {
				    	
				    	ArrayList<NmpWifiInfo> password_list= mWifiManage.PrefPasswordGet();
				    	if(password_list == null || password_list.size() == 0){
				    		textViewString="password list isempty";
				    	} else {
					    	textViewString="";					    	
					    	for(NmpWifiInfo info : password_list) {				    		
					    		textViewString += "[SSID,Password]=" + info.ssid +","+ info.password +"\n";				    		
					    	}
				    	}
				    	mTextView.setText(textViewString);
				    	Random myRandom = new Random();
				    	mWifiManage.PrefPasswordSet(String.valueOf(myRandom.nextInt()) , String.valueOf(myRandom.nextInt()));
				    	mWifiManage.PrefPasswordSet(String.valueOf(myRandom.nextInt()), String.valueOf(myRandom.nextInt()));
				    	mWifiManage.PrefPasswordSave();
				    	
				    } else if (mSelectString.equals(NMP_TEST_RECONNECT_SSID)) {
				    	//mWifiManage.ReConnectToSSID("Jericho");		
				    	
				    	CWifiConfigDlg dlg = new CWifiConfigDlg(m_context){
				    		public void Result(NmpWifiInfo wifiinfo) {
				    			if(wifiinfo == null) {
				    				textViewString="Cancel";
				    				mTextView.setText(textViewString);	
				    			} else {
									textViewString="";
									textViewString += "[ssid]:"+ wifiinfo.ssid +"\n";
									textViewString += "[protect]:"+ wifiinfo.protect +"\n";
									textViewString += "[password]:"+ wifiinfo.password +"\n";
									mTextView.setText(textViewString);	
									mWifiManage.ReConnectToSSID(wifiinfo.ssid);						
				    			}
				    		}				    		
				    	};
				    	dlg.SetProtectEnable(false);
				    	dlg.SetPasswordEnable(false);
				    	dlg.SetSSIDEnable(true);
				    	dlg.show();
				    	
				    } else if (mSelectString.equals(NMP_TEST_ALARMDIALOG)) {
				    	CWifiConfigDlg dlg = new CWifiConfigDlg(m_context){
				    		public void Result(NmpWifiInfo wifiinfo) {
				    			
				    			if(wifiinfo == null) {
				    				textViewString="Cancel";
				    				mTextView.setText(textViewString);	
				    			} else {				    			
									textViewString="";
									textViewString += "[ssid]:"+ wifiinfo.ssid +"\n";
									textViewString += "[protect]:"+ wifiinfo.protect +"\n";
									textViewString += "[password]:"+ wifiinfo.password +"\n";				    			
					    			mTextView.setText(textViewString);
				    			}
				    		}				    		
				    	};
				    	dlg.SetPassword("default password");
				    	dlg.SetProtect("WEP");
				    	dlg.SetSSID("default SSID");
				    	dlg.SetProtectEnable(false);
				    	dlg.SetSSIDEnable(false);
				    	dlg.show();
				    } else if (mSelectString.equals(NMP_TEST_UDP)) {
				    	udpRemoteClientTestStart();
				    } else if (mSelectString.equals(NMP_TEST_TCP)) {	
				    	tcpRemoteClientTestStart();
				    } else {
						textViewString = " Unhandle selection:" + mSelectString ;									    
				    }
					mTextView.setText(textViewString);	
			}
		
		});           

        mButtonSend.setOnClickListener(new Button.OnClickListener() {
        	public void onClick(View v) {
				
			    textViewString = " Send the command:" + mSelectString + "\n";
				if(mSelectString.equals(NMP_TEST_CONNECT)) {
			    } else if (mSelectString.equals(NMP_TEST_DISCONNECT)) {
			    } else if (mSelectString.equals(NMP_TEST_IDENTIFY)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFILIST)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFICONNECT)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFIDISCONNECT)) {
			    } else if (mSelectString.equals(NMP_TEST_DEVICEINFO)) {
			    } else if (mSelectString.equals(NMP_TEST_WIFICONFIG)) {		
			    } else if (mSelectString.equals(NMP_TEST_WIFIISCONNECTED)) {
			    } else if (mSelectString.equals(NMP_TEST_GET_SSIDPASSWORD_LIST)) {

			    } else if (mSelectString.equals(NMP_TEST_RECONNECT_SSID)) {
			  
			    } else if (mSelectString.equals(NMP_TEST_ALARMDIALOG)) {
			    } else if (mSelectString.equals(NMP_TEST_UDP)) {
			    	if(mUdpClient != null) {
			    		mUdpClient.SendString("I am mUdpClient","192.168.49.1",3912);
			    	} else {
			    		 textViewString += " press start to create connect first !!" ;
			    	}
			    } else if (mSelectString.equals(NMP_TEST_TCP)) {	
			    	if(mTcpClient != null) {
			    		mTcpClient.SendString("I am mTcpClient");
			    	} else {
			    		textViewString += " press start to create connect first !!" ;
			    	}
			    } else {
					textViewString = " Unhandle selection:" + mSelectString ;									    
			    }
				mTextView.setText(textViewString);	
		}
 		});
 		
        mButtonStop.setOnClickListener(new Button.OnClickListener()
 		{
 			public void onClick(View v) {
 				
 				    textViewString = " start the command:" + mSelectString ;
 					if(mSelectString.equals(NMP_TEST_CONNECT)) {
 				    } else if (mSelectString.equals(NMP_TEST_DISCONNECT)) {
 				    } else if (mSelectString.equals(NMP_TEST_IDENTIFY)) {
 				    } else if (mSelectString.equals(NMP_TEST_WIFILIST)) {
 				    } else if (mSelectString.equals(NMP_TEST_WIFICONNECT)) {	
 				    } else if (mSelectString.equals(NMP_TEST_WIFIDISCONNECT)) {
 				    } else if (mSelectString.equals(NMP_TEST_DEVICEINFO)) {
 				    } else if (mSelectString.equals(NMP_TEST_WIFICONFIG)) {		
 				    } else if (mSelectString.equals(NMP_TEST_WIFIISCONNECTED)) {
 				    } else if (mSelectString.equals(NMP_TEST_GET_SSIDPASSWORD_LIST)) {
 				    } else if (mSelectString.equals(NMP_TEST_RECONNECT_SSID)) {				    	
 				    } else if (mSelectString.equals(NMP_TEST_ALARMDIALOG)) {
 				    } else if (mSelectString.equals(NMP_TEST_UDP)) {
 				    	textViewString = "disconnect!!";
 				    	udpRemoteClientTestStop();
 				    } else if (mSelectString.equals(NMP_TEST_TCP)) {
 				    	textViewString = "disconnect!!";
 				    	tcpRemoteClientTestStop();
 				    } else {
 						textViewString = " Unhandle selection:" + mSelectString ;									    
 				    }
 					mTextView.setText(textViewString);	
 			}
 		
 		});           

		m_busy_dialog = new ProgressDialog(this);  
		m_busy_dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		m_busy_dialog.setTitle(this.getString(R.string.get_gateway_device_list));
		m_busy_dialog.setIndeterminate(false);
		m_busy_dialog.setCancelable(true);
		m_busy_dialog.setButton(ProgressDialog.BUTTON_NEUTRAL,"Cancel",new DialogInterface.OnClickListener(){  
			  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.cancel();  
            } 
        });
		
        Log.d(this.toString(),"NmpTest onCreate end");
    }
	
	@Override
	protected void onDestroy() {
		Log.d(this.toString(),"onDestroy start");
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d(this.toString(),"onDestroy stop");
	}
	@Override
	protected void onPause() {
		Log.d(this.toString(),"onPause start");
		// TODO Auto-generated method stub
		//udpTestStop();
		//TcpTestStop();
		//WifiConnectStop();
		//WifiScanStop();
		//JsonCommandStop();
		//NmpDeviceClose();
		
		super.onPause();
		Log.d(this.toString(),"onPause stop");
	}
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		Log.d(this.toString(),"onPostCreate start");
		// TODO Auto-generated method stub
		super.onPostCreate(savedInstanceState);
		Log.d(this.toString(),"onPostCreate stop");
	}
	@Override
	protected void onPostResume() {
		Log.d(this.toString(),"onPostResume start");
		// TODO Auto-generated method stub
		super.onPostResume();
		Log.d(this.toString(),"onPostResume stop");
	}
	@Override
	protected void onRestart() {
		Log.d(this.toString(),"onRestart start");
		// TODO Auto-generated method stub
		super.onRestart();
		Log.d(this.toString(),"onRestart stop");
	}
	@Override
	protected void onResume() {
		Log.d(this.toString(),"onResume start");
		// TODO Auto-generated method stub
		
		textViewString = "Test Start ....";
        mTextView.setText(textViewString); 
        //udpTestStart(); 	
        //TcpTestStart(); 	
        //WifiConnectStart();
        //WifiScanStart();
        //JsonCommandStart();
        //DialogTest();		
		super.onResume();
		Log.d(this.toString(),"onResume stop");
	}
	@Override
	protected void onStart() {
		Log.d(this.toString(),"onStart start");
		// TODO Auto-generated method stub
		super.onStart();

		Log.d(this.toString(),"onStart stop");
	}
	@Override
	protected void onStop() {
		Log.d(this.toString(),"onStop start");
		// TODO Auto-generated method stub

		super.onStop();
		Log.d(this.toString(),"onStop stop");
	}
	
	private boolean CheckConnected() {
		if(IsState(CONTROL_STATE.READY)) {
			return true;
		} else {
			new AlertDialog.Builder(this)
			.setMessage("Disconnected, please connect to dongle first...")
			.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					//setResult(RESULT_OK);
					//finish();
				}
			})
			.show();
			return false;
		}
	}
	
	private void ConnectResultHandle(boolean connect) {
    	if(connect){
    		SetToState(CONTROL_STATE.READY);
			textViewString="Connected";
			mTextView.setText(textViewString);	
			Toast.makeText(this, "Connected", Toast.LENGTH_LONG).show();
    	} else {
    		SetToState(CONTROL_STATE.NONE);
			textViewString="Disconnected";
			mTextView.setText(textViewString);
			Toast.makeText(this, "Disconnected", Toast.LENGTH_LONG).show();
    	}
	}
	 
	private void NmpDeviceOpen() {
    	
		NmpDeviceClose();
		
		mNmpDevice = new CNmpDongle(this)
		{			
			public void DeviceStatus(int status){
				
				if(m_f_status == status)
					return;
				
				m_f_status = status;
				
				switch(status) {
					case CNmpDevice.DEVICE_STATUS_DISCONNECT:
						ConnectResultHandle(false);
						break;
					case CNmpDevice.DEVICE_STATUS_CONNECT:
						ConnectResultHandle(true);
						break;
				}
				
			}
			
			public void GetIdentifyResponse(NmpCmdIdentifyInfo info) {
				textViewString="";
				textViewString += "[id]:"+ info.id +"\n";
				textViewString += "[session]:"+ info.session +"\n";
				textViewString += "[command]:"+ info.command +"\n";
				textViewString += "[version]:"+ info.version +"\n";
				mTextView.setText(textViewString);	
			}
			
			public void GetWifiListResponse(NmpCmdWifiList info) {
				textViewString = "";
				int index=0;
				ArrayList<NmpWifiInfo> wifilist = info.list;
				Iterator<NmpWifiInfo> iterator = wifilist.iterator();
		        while(iterator.hasNext()) {
		        	NmpWifiInfo wifiinfo = (NmpWifiInfo) iterator.next();
		        	textViewString += "("+index+") [ssid]:" + wifiinfo.ssid + " [protect]:" + wifiinfo.protect 
		        			+ " [password]:" + " [status]:" + wifiinfo.status + "\n";
		        	index++;		        	     	
		        }
		        mTextView.setText(textViewString);
		        m_busy_dialog.dismiss();
			}

			public void GetWifiConnectResponse(NmpCmdBase info) {
				textViewString="";
				textViewString += "[id]:"+ info.id +"\n";
				textViewString += "[session]:"+ info.session +"\n";
				textViewString += "[command]:"+ info.command +"\n";
				mTextView.setText(textViewString);					
			}
			
			public void GetWifiDisConnectResponse(NmpCmdBase info) {
				textViewString="";
				textViewString += "[id]:"+ info.id +"\n";
				textViewString += "[session]:"+ info.session +"\n";
				textViewString += "[command]:"+ info.command +"\n";
				mTextView.setText(textViewString);					
			}
			
			public void GetDeviceInfoResponse(NmpCmdDeviceInfo info) {
				textViewString="";
				textViewString += "[id]:"+ info.id +"\n";
				textViewString += "[session]:"+ info.session +"\n";
				textViewString += "[command]:"+ info.command +"\n";
				textViewString += "[name]:"+ info.name +"\n";
				textViewString += "[ssid]:"+ info.ssid +"\n";
				textViewString += "[mac]:"+ info.mac +"\n";
				textViewString += "[password]:"+ info.password +"\n";
				textViewString += "[major version]:"+ info.firmware_majon +"\n";
				textViewString += "[minor version]:"+ info.firmware_minor +"\n";
				mTextView.setText(textViewString);					
			}	

			public void GetWifiConfigResponse(NmpCmdBase info) {
				textViewString="";
				textViewString = "[id]:"+ info.id +"\n";
				textViewString = "[session]:"+ info.session +"\n";
				textViewString = "[command]:"+ info.command +"\n";
				mTextView.setText(textViewString);						
			}	
			
			public void GetWifiIsConnectedResponse(NmpCmdIsConnected info) {
				textViewString="";
				textViewString += "[id]:"+ info.id +"\n";
				textViewString += "[session]:"+ info.session +"\n";
				textViewString += "[command]:"+ info.command +"\n";
				textViewString += "[isconnected]:"+ info.isconnected +"\n";
				mTextView.setText(textViewString);						
			}	
			
		};   
		
		if(!mNmpDevice.Connect())
		{
			m_f_status = CNmpDevice.DEVICE_STATUS_DISCONNECT;
			ConnectResultHandle(false);				
		}
		
		
    }
	
    private void NmpDeviceClose() {
    	if(mNmpDevice != null) {
    		mNmpDevice.Disconnect();
    		mNmpDevice = null;
    	}    	
    	
    }   	
	   
	
	private void JsonCommandStart(){
		mCmd = new CNmpJsonCmd();
        mButtonStart.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) {
				//textViewString = "";
				//textViewString = mCmd.IdentifyRequestGenerate(android.os.Build.MODEL, 1000);
				
				NmpCmdBase cmd_base = mCmd.Parse(((CNmpJsonCmd)mCmd).response_wifilist_str);
				
				switch(cmd_base.id) {
				
					case CNmpCmd.NMP_CMD_ID_IDENTIFY:
						NmpCmdIdentifyInfo info = (NmpCmdIdentifyInfo)cmd_base;					
						textViewString = "command:" + info.command;
						textViewString += "\n name:" + info.name;
						break;
						
					case CNmpCmd.NMP_CMD_ID_WIFILIST:
						NmpCmdWifiList wiflist = (NmpCmdWifiList)cmd_base;
						textViewString="\n";
						Iterator<NmpWifiInfo> iterator = wiflist.list.iterator();
						NmpWifiInfo wifiinfo;
				        while(iterator.hasNext()) 
				        {
				        	wifiinfo = (NmpWifiInfo) iterator.next();
				        	textViewString += "\n item:" + wifiinfo.ssid +","+ wifiinfo.protect +","+ wifiinfo.strength +","+ wifiinfo.status;
				        }
						break;
						
					case CNmpCmd.NMP_CMD_ID_WIFICONNECT:
						break;
						
					case CNmpCmd.NMP_CMD_ID_WIFIDISCONNECT:
						break;
						
					case CNmpCmd.NMP_CMD_ID_DEVICEINFO:
						break;
						
					case CNmpCmd.NMP_CMD_ID_WIFICONFIG:
						break;
						
					default:
						break;
				}
				mTextView.setText(textViewString);	
			}			
		});   		
	}
	private void JsonCommandStop(){
		
	}	
	private void WifiScanStart(){
		mNmpDevice = new CNmpDongle(this){
			public void ScanFinish(ArrayList<CNmpDeviceItem> target_list){
				textViewString = "";
				for(CNmpDeviceItem item: target_list){
					textViewString+= item.name + "\n";
				}	
				mTextView.setText(textViewString);	
			}			
		};
		
        mButtonStart.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				textViewString = "";
				mTextView.setText(textViewString);
				if(mNmpDevice.Scan())
				{
		            textViewString += "mNmpDevice.Connect() Success";		
				}
				else
				{
					textViewString += "mNmpDevice.Connect() failed";
				}
				mTextView.setText(textViewString);	
			}
			
		});   
        
        mButtonStop.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				textViewString = "Stop Test ....";
				mTextView.setText(textViewString);
			}
		});  		
		
	}
	private void WifiScanStop(){
		mNmpDevice = null;
	}	
	private void WifiConnectStart(){

		mNmpDevice = new CNmpDongle(this)
		{			
			public void DeviceStatus(int status){
				if(m_f_status == status)
					return;
				m_f_status = status;
				switch(status) {
					case DEVICE_STATUS_DISCONNECT:
						textViewString += "Disconnected ...\n";
						mTextView.setText(textViewString);	
						break;
					case DEVICE_STATUS_CONNECT:
						textViewString += "Connected ...\n";
						mTextView.setText(textViewString);	
						break;
				}
				
			}
		};

		
        mButtonStart.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				m_f_status = -1;
				textViewString = "Try to connect nmp device...\n";
				mTextView.setText(textViewString);
	
				if(!mNmpDevice.Connect())
				{
					textViewString += "Wifi is not enabled...";
					mTextView.setText(textViewString);					
				}
			}
			
		});   
        
        mButtonStop.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				textViewString = "Stop Test ....";
				mTextView.setText(textViewString);
				mNmpDevice.Disconnect();		
			}
		});   
        /*
        mButtonSend.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				textViewString = "Send message test ....";
				mTextView.setText(textViewString);
				mNmpDevice.SendMessage("Walace Good!!");	
			}
		});
		*/           
	}
	private void WifiConnectStop(){
		mNmpDevice.Disconnect();
		mNmpDevice = null;
	}	
	
    private void udpTestStart()
    {    	
        mUdpServer = new CNmpUdp(7878)
        {
        	public void GetString(String str, String from, int port)
        	{
        		//Log.d(this.toString(),"Client get response string"+str+" from "+from);
                textViewString += "mUdpServer get response string \""+str+"\" from \""+from+":" + port + "\"";
                mTextView.setText(textViewString);
        	}
        };         
        
        mUdpClient = new CNmpUdp(7879)
        {
        	public void GetString(String str, String from, int port)
        	{
        		//Log.d(this.toString(),"Client get response string"+str+" from "+from);
                textViewString += "mUdpServer get response string \""+str+"\" from \""+from+":" + port + "\"";
                mTextView.setText(textViewString);
        	}
        };  
        
        if(!mUdpServer.start())
        {
        	Log.e(this.toString(),"mUdpServer start error!");
        }
        
        if(!mUdpClient.start())
        {
        	Log.e(this.toString(),"mUdpClient start error!");
        }
        
        mButtonStart.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				mTextView.setText("");
				mUdpClient.SendString("I am mUdpClient",null,7878);
				mUdpServer.SendString("I am mUdpServer",null,7879);
			}
			
		});   
        
        mButtonStop.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				textViewString = "";
				mTextView.setText(textViewString);
			}
		});     		
    }
    
    private void udpTestStop()
    {
		mUdpClient.close();
		mUdpServer.close();
		textViewString = "";
		mTextView.setText(textViewString);
    }
    
    private void TcpTestStart()
    {
        mTcpServer = new CNmpTcp(CNmpTcp.SERVER,null,7900)
        {
        	public void GetString(String str, String from, int port)
        	{
        		//Log.d(this.toString(),"Client get response string"+str+" from "+from);
                textViewString += "mTcpServer get response string \""+str+"\" from \""+from+":" + port + "\"\n";
                mTextView.setText(textViewString);
        	}
        	
        	public void UpdateStatus(int status, String address, int port)
        	{
        		//Log.d(this.toString(),"Client get response string"+str+" from "+from);
                textViewString += "mTcpServer get status \""+status+"\" from \""+address+":" + port + "\"\n";
                mTextView.setText(textViewString);
        	}        	
        };         
        
        mTcpClient = new CNmpTcp(CNmpTcp.CLIENT,null,7900)
        {
        	public void GetString(String str, String from, int port)
        	{
        		//Log.d(this.toString(),"Client get response string"+str+" from "+from);
                textViewString += "mTcpClient get response string \""+str+"\" from \""+from+":" + port + "\"\n";
                mTextView.setText(textViewString);
        	}
        	
        	public void UpdateStatus(int status, String address, int port)
        	{
        		//Log.d(this.toString(),"Client get response string"+str+" from "+from);
                textViewString += "mTcpClient get status \""+status+"\" from \""+address+":" + port + "\"\n";
                mTextView.setText(textViewString);
        	}           	
        };  
        
        mTcpServer.start();        
        
        mButtonStart.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				mTcpClient.start();
				textViewString = "";
				mTextView.setText(textViewString);				

			}
			
		});   
        
        mButtonStop.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				TcpTestStop();
			}
		});     
        /*
        mButtonSend.setOnClickListener(new Button.OnClickListener()
		{
			public void onClick(View v) 
			{
				mTextView.setText("");
				mTcpClient.SendString("I am mTcpClient");
				mTcpServer.SendString("I am mTcpServer");
			}
		});      
		*/     
    }
    
    private void TcpTestStop()
    {
    	mTcpServer.close();
    	mTcpClient.close();
		textViewString = "";
		mTextView.setText(textViewString);
    }
   
    private void udpRemoteClientTestStart()
    {    	
    	if(mUdpClient != null)
    		return;
    	
        mUdpClient = new CNmpUdp(23912)
        {
        	public void GetString(String str, String from, int port)
        	{
        		//Log.d(this.toString(),"Client get response string"+str+" from "+from);
                textViewString += "mUdpServer get response string \""+str+"\" from \""+from+":" + port + "\"";
                mTextView.setText(textViewString);
        	}
        };  

        if(!mUdpClient.start())
        {
        	Log.e(this.toString(),"mUdpClient start error!");
        }

    }
    
    private void udpRemoteClientTestStop() {    
    	if(mUdpClient == null)
    		return;    	
    	mUdpClient.close();
    	mUdpClient = null;
    }  
    
    private void tcpRemoteClientTestStart()
    {    	
    	if(mTcpClient != null)
    		return;
    	
    	mTcpClient = new  CNmpTcp(false,"192.168.49.1",3912)
        {
        	public void GetString(String str, String from, int port)
        	{
        		//Log.d(this.toString(),"Client get response string"+str+" from "+from);
                textViewString += "mUdpServer get response string \""+str+"\" from \""+from+":" + port + "\"";
                mTextView.setText(textViewString);
        	}
        };  
        mTcpClient.setTimeout(0,5000, true);
        mTcpClient.start();
    }
    
    private void tcpRemoteClientTestStop() {    
    	if(mTcpClient == null)
    		return;
    	
    	mTcpClient.close();
    	mTcpClient = null;
    }      
    
	private void SetToState(CONTROL_STATE state) {
		Log.e(this.toString(),"Current State:" + state);		
		mState = state;
	}    
	
	private boolean IsState(CONTROL_STATE state) {
		Log.e(this.toString(),"Current State:" + mState + " < === > Check State:" + state );			
		return (mState == state);
	}   
}